const multer = require('multer')
const path = require('path')
// var upload = multer({ dest: 'upload/' });

// 通过 filename 属性定制
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, path.join(__dirname, '../../../upload')) // 保存的路径，备注：需要自己创建
  },
  filename: function (req, file, cb) {
    console.log('🚀 ~ file: upload.js ~ line 10 ~ file', file)
    // 将保存文件名设置为 字段名 + 时间戳，比如 logo-1478521468943

    cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname))
  }
})

// 通过 storage 选项来对 上传行为 进行定制化
const upload = multer({ storage: storage })

module.exports = upload
